﻿(function (id) {
    var Map = {
        initMap: function () {
            this.createMap();//创建地图
            this.setMapEvent();//设置地图事件
            this.addMapControl();//向地图添加控件
           
        },
        //创建地图函数：
        createMap: function () {
            var map = new BMap.Map(id, { minZoom: 10, maxZoom: 18 }); //在百度地图容器中创建一个地图
            var point = new BMap.Point(111.292827, 30.697726); //定义一个中心点坐标
            map.centerAndZoom(point, 10); //设定地图的中心点和坐标并将地图显示在地图容器中
            window.map = map; //将map变量存储在全局
        },
        //地图事件设置函数：
        setMapEvent: function () {
            map.enableDragging(); //启用地图拖拽事件，默认启用(可不写)
            map.enableScrollWheelZoom(); //启用地图滚轮放大缩小
            map.enableDoubleClickZoom(); //启用鼠标双击放大，默认启用(可不写)
            map.enableKeyboard(); //启用键盘上下左右键移动地图
        },
        //地图控件添加函数：
        addMapControl: function () {
            //向地图中添加缩放控件
            var ctrl_nav = new BMap.NavigationControl({
                anchor: BMAP_ANCHOR_TOP_LEFT,
                type: BMAP_NAVIGATION_CONTROL_LARGE
            });
            map.addControl(ctrl_nav);
            //向地图中添加缩略图控件
            var ctrl_ove = new BMap.OverviewMapControl({
                anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
                isOpen: 1
            }); map.addControl(ctrl_ove); //向地图中添加比例尺控件
            var ctrl_sca = new BMap.ScaleControl({
                anchor: BMAP_ANCHOR_BOTTOM_LEFT
            });
            map.addControl(ctrl_sca);
            map.addControl(new BMap.MapTypeControl({ mapTypes: [BMAP_NORMAL_MAP, BMAP_HYBRID_MAP] }));
        }
    };
    Map.initMap();
    //站点数据
    //var addr_tree = [{"monitoring_point_id":"Z0060902100","monitoring_point_name":"万寿桥","monitoring_point_mn":"0060902100","monitor_type_id":"21","monitoring_point_type":"1","monitoring_point_longitude":"111.322518","monitoring_point_latitude":"30.682308","monitoring_point_county":"43050200","monitoring_point_pic":"/MnIcon/a267e4edd47348aaaca7f2128cc89de1.png","monitoring_point_address":"湖北省宜昌市伍家岗区沿江大道","monitoring_point_auto_alarm":"1","CommentName":"伍家岗区"},{"monitoring_point_id":"Z0060902120","monitoring_point_name":"万达广场","monitoring_point_mn":"0060902120","monitor_type_id":"21","monitoring_point_type":"0","monitoring_point_longitude":"111.314502","monitoring_point_latitude":"30.686059","monitoring_point_county":"43050200","monitoring_point_pic":"/MnIcon/6406ff09a1ca45c6b21bcd19d9d6c072.png","monitoring_point_address":"湖北省宜昌市伍家岗区沿江大道(宜昌港汽车客运东站对面)","monitoring_point_auto_alarm":"1","CommentName":"伍家岗区"},{"monitoring_point_id":"Z0060902140","monitoring_point_name":"隆康路","monitoring_point_mn":"0060902140","monitor_type_id":"21","monitoring_point_type":"2","monitoring_point_longitude":"111.301139","monitoring_point_latitude":"30.695404","monitoring_point_county":"43050100","monitoring_point_pic":"/MnIcon/75b355961db04c63bb968933716a580c.png","monitoring_point_address":"湖北省宜昌市西陵区隆康路湖北银行","monitoring_point_auto_alarm":"1","CommentName":"西陵区"},{"monitoring_point_id":"1605190474","monitoring_point_name":"黄河路","monitoring_point_mn":"1605190474","monitor_type_id":"21","monitoring_point_type":"2","monitoring_point_longitude":"111.321605","monitoring_point_latitude":"30.735283","monitoring_point_county":"43050100","monitoring_point_pic":"/MnIcon/f664ddfc702f49c48750c83dcc41c675.png","monitoring_point_address":"湖北省宜昌市西陵区","monitoring_point_auto_alarm":"1","CommentName":"西陵区"},{"monitoring_point_id":"1605190541","monitoring_point_name":"中心医院","monitoring_point_mn":"1605190541","monitor_type_id":"21","monitoring_point_type":"0","monitoring_point_longitude":"111.312205","monitoring_point_latitude":"30.689601","monitoring_point_county":"43050200","monitoring_point_pic":"/MnIcon/f8525703a46f4e94811034618f181b8e.png","monitoring_point_address":"宜昌市伍家岗区夷陵大道183号 ","monitoring_point_auto_alarm":"1","CommentName":"伍家岗区"},{"monitoring_point_id":"1605190552","monitoring_point_name":"五一广场","monitoring_point_mn":"1605190552","monitor_type_id":"21","monitoring_point_type":"0","monitoring_point_longitude":"111.365112","monitoring_point_latitude":"30.650647","monitoring_point_county":"43050200","monitoring_point_pic":"/MnIcon/92d5adacc1184f3aafd8ba11f26e4911.png","monitoring_point_address":"宜昌市伍家岗区伍临路五一广场南大门","monitoring_point_auto_alarm":"1","CommentName":"伍家岗区"},{"monitoring_point_id":"1605190479","monitoring_point_name":"北山坡","monitoring_point_mn":"1605190479","monitor_type_id":"21","monitoring_point_type":"3","monitoring_point_longitude":"111.311558","monitoring_point_latitude":"30.694099","monitoring_point_county":"43050200","monitoring_point_pic":"/MnIcon/f846d2a566ad4f61af953f8c8d0e68cc.png","monitoring_point_address":"宜昌市伍家岗区胜利三路消防支队","monitoring_point_auto_alarm":"1","CommentName":"伍家岗区"},{"monitoring_point_id":"1605190550","monitoring_point_name":"三峡大学","monitoring_point_mn":"1605190550","monitor_type_id":"21","monitoring_point_type":"2","monitoring_point_longitude":"111.317889","monitoring_point_latitude":"30.719408","monitoring_point_county":"43050100","monitoring_point_pic":"/MnIcon/c384d13fb10348a7a4c82f6c07ae2a84.png","monitoring_point_address":"湖北省宜昌市西陵区大学路8号","monitoring_point_auto_alarm":"1","CommentName":"西陵区"},{"monitoring_point_id":"1605190500","monitoring_point_name":"北苑桥","monitoring_point_mn":"1605190500","monitor_type_id":"21","monitoring_point_type":"1","monitoring_point_longitude":"111.317942","monitoring_point_latitude":"30.718288","monitoring_point_county":"43050100","monitoring_point_pic":"/MnIcon/7efd8e37c4ff4d3a8be4c5642e89b8e1.png","monitoring_point_address":"湖北省宜昌市西陵区行政服务中心","monitoring_point_auto_alarm":"1","CommentName":"西陵区"},{"monitoring_point_id":"1605190523","monitoring_point_name":"宜昌东站","monitoring_point_mn":"1605190523","monitor_type_id":"21","monitoring_point_type":"0","monitoring_point_longitude":"111.377191","monitoring_point_latitude":"30.66441","monitoring_point_county":"43050200","monitoring_point_pic":"/MnIcon/7fb77ca5ec2d4faa91b27ba80895ff57.png","monitoring_point_address":"宜昌市伍家岗区桔城路","monitoring_point_auto_alarm":"1","CommentName":"伍家岗区"},{"monitoring_point_id":"1605190530","monitoring_point_name":"果园一路","monitoring_point_mn":"1605190530","monitor_type_id":"21","monitoring_point_type":"0","monitoring_point_longitude":"111.302253","monitoring_point_latitude":"30.70491","monitoring_point_county":"43050100","monitoring_point_pic":"/MnIcon/abe2c8aaf6944a91bfc7cf774b086975.png","monitoring_point_address":"宜昌市西陵区果园一路3号","monitoring_point_auto_alarm":"1","CommentName":"西陵区"}]
    var mnType = {
    "PP": "雨量站",
    "ZZ": "水位站",
    "ZQ": "水文站",
    "RR": "水库",
    "SS": "墒情站",
    "ZG": "地下水站"
    };
    var imgType = {
        "PP": "../static/h-ui.admin/images/map_yuliang.png",
        "ZZ": "../static/h-ui.admin/images/map_shuiwei.png",
        "ZQ": "../static/h-ui.admin/images/map_shuiwen.png",
        "RR": "../static/h-ui.admin/images/map_shuiku.png",
        "SS": "../static/h-ui.admin/images/map_shangqing.png",
        "ZG": "../static/h-ui.admin/images/map_dixiashui.png"
    };

    //调用增加点数据方法
    $.get("../../Home/GetAllSite", "", function (data) {
        data = JSON.parse(data);
        addMarker(data, 60, 60);
    },"json")
    
    //增加点数据方法
    function addMarker(markerArr1, width, height) {
            for (var i = 0; i < markerArr1.length; i++) {
                var json = markerArr1[i];
                var p0 = json.LGTD;
                var p1 = json.LTTD;

                var point = new BMap.Point(p0, p1);
                var icon = { h: height, l: 0, lb: 9, t: 1, w: width, x: 6 };
                var iconImg = imgType[json.STTP];
                var myIcon2 = new BMap.Icon(iconImg, new BMap.Size(50, 50));
                var marker = new BMap.Marker(point, { icon: myIcon2 });;

                var iw = createInfoWindow(markerArr1, i);
                var label = new BMap.Label(json.STNM, { "offset": new BMap.Size(icon.lb - icon.x + 15, -2) });
                marker.setLabel(label);
                map.addOverlay(marker);
                label.setStyle({ borderColor: "#808080", color: "#333", cursor: "pointer" });
                (function () {
                    var index = i;
                    var _iw = createInfoWindow(markerArr1, i);
                    var _marker = marker;
                    _marker.addEventListener("click", function (img) {

                        this.openInfoWindow(_iw);
                    });
                    _iw.addEventListener("open", function () {
                        _marker.getLabel().hide();
                    });
                    _iw.addEventListener("close", function () { _marker.getLabel().show(); });
                    label.addEventListener("click", function () { _marker.openInfoWindow(_iw); });
                    if (!!json.isOpen) {
                        label.hide();
                        _marker.openInfoWindow(_iw);
                    }
                })()
            }
    }
    //增加点数据信息窗口
    function createInfoWindow(markerArr1, i) {

            var json = markerArr1[i];
            var data = json.monitoring_point_mn; //加载点的信息
            var tr = "";
            var sj = "";
            $(data).each(function (index, row) {

                //tr += "<tr><td>" + row.factor_name + "</td><td>" + row.datatime + "</td><td>" + row.avgvalue + "</td></tr>";
                tr += row.factor_name + ":" + row.avgvalue + "<br />";
                sj = row.datatime;
            });
            var opts = {
                width: 700,     // 信息窗口宽度
                height: 200,     // 信息窗口高度
                title: "", // 信息窗口标题
                enableMessage: true,//设置允许信息窗发送短息
                message: ''
            }
            var detail = json.STNM +
                 "<br>"  + mnType[json.STTP] +
                "<br/>" + json.STLC;
            var imgDom = "<img style='float:left;margin-left: 27px;margin-top:15px;width:156px;height:165px;' src='' /><div style='float:left;font-size: 17px;margin-left: 20px;width:370px;line-height:24px;'>" + detail + "</div>";
            var htmldom = imgDom + "<div style='float:left;font-size: 17px;margin-left: 20px;width:370px;overflow-x: auto;line-height:24px;'>" + "时间：" + sj + "<br />" + tr + "</div>";
            var iw = new BMap.InfoWindow(htmldom, opts);
            return iw;
    }


})("allmap");